const router = require('koa-router')()
//模拟的数据
let data=[
   {
     name:'王某某',
     age:18,
     id:18382100331,
   },
   {
    name:'韩某某',
    age:19,
    id:18382100332,
  },
  {
    name:'高某某',
    age:16,
    id:18382100333,
  }
]

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})


//请求总数据接口
router.get('/api/data', async (ctx, next) => {
  ctx.body = {
    code:200,  //code码
    data       //数据
  }
})

//增加请求的数据接口
router.post('/api/add', async (ctx, next) => {
  
  //console.log(ctx.request.body,"666");
  data.push(ctx.request.body);
  ctx.body = {
    code:200,  //code码
    data
  }
})

//删除数据的请求接口
router.get('/api/deletes/:id', async (ctx,next)=>{
  let {id}=ctx.params;
  let newData=[];
  //console.log(id,'id');
  data.forEach(item=>
    {
      if(item.id==id){
        //console.log(item,"item");
        
      }else{
        newData.push(item);
      }
    })
    data=newData;
    ctx.body = {
      code:200,  //code码
      data       //数据
    }
  
//修改请求的数据接口
router.get('/api/change', async (ctx, next) => {
  
  ctx.body = {
    code:200,  //code码
    
  }
})
     


//查询请求的数据接口
router.get('/api/search', async (ctx, next) => {
  
  ctx.body = {
    code:200,  //code码
    
  }
})
   
})



module.exports = router
